package com.lenovo.agricultureclient;

import com.lenovo.agricultureclient.bean.ControllerStatus;
import com.lenovo.agricultureclient.bean.SensorConfig;
import com.lenovo.agricultureclient.bean.SensorValue;
import com.lenovo.agricultureclient.request.BaseRequest.RequestResult;
import com.lenovo.agricultureclient.request.RequestThread;
import com.lenovo.agricultureclient.util.Util;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;

public class ClientApp extends Application 
{
	/*
	 * SharedPreferences是以键值对来存储应用程序的配置信息的一种方式，
	 * 它只能存储基本数据类型。一个程序的配置文件仅可以在本应用程序中使用，
	 * 或者说只能在同一个包内使用，不能在不同的包之间使用。
	 * 实际上sharedPreferences是采用了XML格式将数据存储到设备中，
	 */
	private SharedPreferences mSharedPreferences;
	//存储服务器IP地址
	private String serverIpStr = "";
	
	//所有传感器的预警值范围，在进入主界面时，主动从服务器端获取
	private SensorConfig mSensorConfig;
	//各个传感器的当前数据值
	private SensorValue mCurSensorValue;
	//所有控制器的状态，在进入手动控制界面和设置界面时，会从服务器上获取
	private ControllerStatus mControllerStatus;
	
	@Override
	public void onCreate()
	{
		super.onCreate();
		initImageLoader(getApplicationContext());
		mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		mSensorConfig = new SensorConfig();
		mCurSensorValue = new SensorValue();
		mControllerStatus = new ControllerStatus();
		readSensorConfig();
	}
	
	public static void initImageLoader(Context context) 
	{
		/**
		 * Android-Universal-Image-Loader是一个开源的UI组件程序，
		 * 目的是提供一个可重复使用的仪器为异步图像加载，缓存和显示
		 */
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)  //设置线程优先级
				.denyCacheImageMultipleSizesInMemory()		//设置文件存储模式
				.discCacheFileNameGenerator(new Md5FileNameGenerator())		//才用md5方式给临时文件令名
				.tasksProcessingOrder(QueueProcessingType.LIFO)		//	设置任务堆栈
				.build();
		ImageLoader.getInstance().init(config);
	}
	
	private Handler mHandler = new Handler() 
	{
		//http或者socket处理线程，在处理完相关数据以后将结果发送到这么
		//在这里先做简单的判断处理，然后再调用相关请求的回调函数
		public void handleMessage(Message msg) 
		{
			switch(msg.what)
			{
			case RequestThread.MSG_REQUEST_RESULT:
				RequestResult result = RequestResult.values()[msg.arg1];
				if(result == RequestResult.RESULT_NO_NET)
				{
					//Toast.makeText(ClientApp.this, ClientApp.this.getResources().getString(R.string.noNetwork), Toast.LENGTH_SHORT).show();
				}
				RequestThread requestThread = (RequestThread)msg.obj;
				if(requestThread != null)
				{
					//调用相关请求的回调函数
					requestThread.hanlderResult(result);
				}
				break;
			default:
				break;
			}
		}
	};
	public Handler getHandler()
	{
		return mHandler;
	}
	
	//获取服务器端的IP地址
	public String getServerIpStr() {
		serverIpStr = readIpStr();//先从配置文件中读起IP地址
		if(serverIpStr.equals(""))//如果配置文件中没有
		{
			try {
				serverIpStr = Util.getLocalIpAddress(this);//再读取本地网络IP地址
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return serverIpStr;
	}
	public void setServerIpStr(String serverIpStr) {
		this.serverIpStr = serverIpStr;
		saveIpStr(this.serverIpStr);
	}



	//保存用户名到配置文件
	public String getUsername() {
		return mSharedPreferences.getString("username","");
	}
	public void setUsername(String username) {
		Editor editor = mSharedPreferences.edit();
		editor.putString("username", username);
		editor.commit();
	}
	
	//保存密码到配置文件
	public String getPassword() {
		return mSharedPreferences.getString("Password","");
	}
	public void setPassword(String alianame) {
		Editor editor = mSharedPreferences.edit();
		editor.putString("Password", alianame);
		editor.commit();
	}
	
	//保存服务器IP地址到配置文件
	private String readIpStr() {
		return mSharedPreferences.getString("IpStr","");
	}
	private void saveIpStr(String ipStr) {
		Editor editor = mSharedPreferences.edit();
		editor.putString("IpStr", ipStr);
		editor.commit();
	}
	
	//保存是否是第一次运行app
	public boolean isFirstRun() {
		return mSharedPreferences.getBoolean("FirstRun",true);
	}
	public void setFirstRun(boolean isFirstRun) {
		Editor editor = mSharedPreferences.edit();
		editor.putBoolean("FirstRun", isFirstRun);
		editor.commit();
	}

	//读取所有传感器预警值的范围
	private void readSensorConfig()
	{
		mSensorConfig.minAirTemperature = mSharedPreferences.getInt("minAirTemperature",mSensorConfig.minAirTemperature);//空气温度最小值
		mSensorConfig.maxAirTemperature = mSharedPreferences.getInt("maxAirTemperature",mSensorConfig.maxAirTemperature);//空气温度最大值
		mSensorConfig.minAirHumidity = mSharedPreferences.getInt("minAirHumidity",mSensorConfig.minAirHumidity);//空气湿度最小值
		mSensorConfig.maxAirHumidity = mSharedPreferences.getInt("maxAirHumidity",mSensorConfig.maxAirHumidity);//空气湿度最大值
		mSensorConfig.minSoilTemperature = mSharedPreferences.getInt("minSoilTemperature",mSensorConfig.minSoilTemperature);//土壤温度最小值
		mSensorConfig.maxSoilTemperature = mSharedPreferences.getInt("maxSoilTemperature",mSensorConfig.maxSoilTemperature);//土壤温度最大值
		mSensorConfig.minSoilHumidity = mSharedPreferences.getInt("minSoilHumidity",mSensorConfig.minSoilHumidity);//土壤湿度最小值
		mSensorConfig.maxSoilHumidity = mSharedPreferences.getInt("maxSoilHumidity",mSensorConfig.maxSoilHumidity);//土壤湿度最大值
		
		mSensorConfig.minLight = mSharedPreferences.getInt("minLight",mSensorConfig.minLight);//灯光强度最小值
		mSensorConfig.maxLight = mSharedPreferences.getInt("maxLight",mSensorConfig.maxLight);//灯光强度最大值
		mSensorConfig.minCo2 = mSharedPreferences.getInt("minCo2",mSensorConfig.minCo2);//CO2浓度最小值
		mSensorConfig.maxCo2 = mSharedPreferences.getInt("maxCo2",mSensorConfig.maxCo2);//CO2浓度最大值
		mSensorConfig.maxPM25 = mSharedPreferences.getInt("maxPM25",mSensorConfig.maxPM25);//PM2.5浓度最大值
		mSensorConfig.controlAuto = mSharedPreferences.getInt("controlAuto",mSensorConfig.controlAuto);//0表示手动控制，1表示自动控制
	}
	
	//保存所有传感器预警值的范围
	public void saveSensorConfig()
	{
		Editor editor = mSharedPreferences.edit();
		editor.putInt("minAirTemperature", mSensorConfig.minAirTemperature);//空气温度最小值
		editor.putInt("maxAirTemperature",mSensorConfig.maxAirTemperature);//空气温度最大值
		editor.putInt("minAirHumidity",mSensorConfig.minAirHumidity);//空气湿度最小值
		editor.putInt("maxAirHumidity",mSensorConfig.maxAirHumidity);//空气湿度最大值
		editor.putInt("minSoilTemperature",mSensorConfig.minSoilTemperature);//土壤温度最小值
		editor.putInt("maxSoilTemperature",mSensorConfig.maxSoilTemperature);//土壤温度最大值
		editor.putInt("minSoilHumidity",mSensorConfig.minSoilHumidity);//土壤湿度最小值
		editor.putInt("maxSoilHumidity",mSensorConfig.maxSoilHumidity);//土壤湿度最大值
		
		editor.putInt("minLight",mSensorConfig.minLight);//灯光强度最小值
		editor.putInt("maxLight",mSensorConfig.maxLight);//灯光强度最大值
		editor.putInt("minCo2",mSensorConfig.minCo2);//CO2浓度最小值
		editor.putInt("maxCo2",mSensorConfig.maxCo2);//CO2浓度最大值
		editor.putInt("maxPM25",mSensorConfig.maxPM25);//PM2.5浓度最大值
		editor.putInt("controlAuto",mSensorConfig.controlAuto);//0表示手动控制，1表示自动控制
		editor.commit();
	}

	public SensorConfig getSensorConfig() {
		return mSensorConfig;
	}
	public SensorValue getCurSensorValue() {
		return mCurSensorValue;
	}
	public ControllerStatus getControllerStatus(){
		return mControllerStatus;
	}
}
